package cn.hp.controller;

import cn.hp.entity.Menu;
import cn.hp.entity.Role;
import cn.hp.service.RoleService;
import cn.hp.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 * /api/role
 */
@RestController
@RequestMapping("/api/role")
@Api(value = "/role", tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/getRole")
    public Object getMenu(Role obj) {
        List<Role> list = roleService.selectRoleList(obj);
        List<Map<String, Object>> Mmap = new ArrayList<>();
        for (Role m : list) {
            if ("0".equals(m.getStatus())) {
                Map map = new HashMap();
//                map.put("id", m.getId() + ":" + m.getRole());
                map.put("id", m.getId());
                map.put("title", m.getRemark());
                Mmap.add(map);
            }
        }
        return Result.success(Mmap);
    }

    /**
     * 条件查询
     */
    @GetMapping
    @ApiOperation(value = "查询",notes = "查询所有公告")
    public Object find(Role obj) {
        return Result.success(roleService.selectRoleList(obj));
    }

    /**
     * 条件+分页查询
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询",notes = "分页查询所有公告")
    public Object findPage(Role obj, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageInfo<Role> pageInfo = roleService.selectRoleList(obj, page, limit);
        return Result.success(pageInfo);
    }


    /**
     * 添加
     */
    @PostMapping
    @ApiOperation(value = "添加",notes = "添加公告")
    public Object add(@RequestBody Role obj) {
        int i = roleService.insertRole(obj);
        return i > 0 ? Result.success("添加成功！") : Result.error("添加失败！");
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改",notes = "修改公告")
    public Object update(@RequestBody Role obj) {
        int i = roleService.updateRole(obj);
        return i > 0 ? Result.success("修改成功！") : Result.error("修改失败！");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "删除公告")
    @RequiresPermissions("system:role:del")
    public Object del(@PathVariable Integer id) {
        int i = roleService.deleteRoleById(id);
        return i > 0 ? Result.success("删除成功！") : Result.error("删除失败！");
    }
}